<template>
  <div class="login-container">
    <!-- 登录页面可以用card卡片布局  -->
    <el-card header="请先登录" class="login-card" style="text-align: center">
      <!-- 阻止表单页面的跳转 -->
      <el-form @submit.native.prevent="handleLogin()">
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit"> 登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {},
    };
  },
  methods: {
    // 处理登录方法
    async handleLogin() {
      const res = await this.$http.post("login", this.form); // 提交数据给后端
      // const token = window.sessionStorage('token', res.data.token) // 保存token值
      sessionStorage.token = res.data.token;
      this.$router.push("/main");
      this.$message({
        type: "success",
        message: "登录成功",
      });
      // console.log(token);
    },
  },
};
</script>

<style>
.login-container {
  width: 30rem;
  margin: 10rem auto;
}
</style>